package com.synjones.zeuscloudapp.util;

import com.synjones.zeus.model.ykt.pay.PayXmlInfo;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author yys
 * Description:
 * @date created in 17:52 2018-04-12
 * Modified by:
 */
public class ReadXmlByDom4j {
    private static ReadXmlByDom4j readXmlByDom4j;
    private ReadXmlByDom4j() {
    }

    public static ReadXmlByDom4j getInstance() {
        if (readXmlByDom4j == null) {
            readXmlByDom4j = new ReadXmlByDom4j();
        }
        return readXmlByDom4j;
    }
 /**
   *
   *  @param path xml文件路径
   *  @param code 组织代码
   *  @author yys
   *  Description:
   *  @date 10:07 2018-04-13
   *  @return
   *  throws
   */
    public PayXmlInfo getPayInfos(String path,String code) {
        PayXmlInfo result = null;
        try {
            String rootPath = System.getProperty("user.dir").replace("\\", "/");
            File file = new File(String.valueOf(rootPath+path));
            SAXReader reader = new SAXReader();
            Document document = reader.read(file);
            Element bookstore = document.getRootElement();
            Iterator storeIt = bookstore.elementIterator();
            List<PayXmlInfo> payInfoList = new ArrayList<>();
            while (storeIt.hasNext()) {
                PayXmlInfo payInfo = new PayXmlInfo();
                Element payInfoElement = (Element) storeIt.next();
                Iterator payInfoIt = payInfoElement.elementIterator();
                while (payInfoIt.hasNext()) {
                    Element child = (Element) payInfoIt.next();
                    String nodeName = child.getName();
                    if (nodeName.equals("name")) {
                        String name = child.getStringValue();
                        payInfo.setName(name);
                    } else if (nodeName.equals("organizationCode")) {
                        String organizationCode = child.getStringValue();
                        payInfo.setOrganizationCode(organizationCode);
                    } else if (nodeName.equals("accType")) {
                        String accType = child.getStringValue();
                        payInfo.setAccType(accType);
                    } else if (nodeName.equals("remark")) {
                        String remark = child.getStringValue();
                        payInfo.setRemark(remark);
                    } else if (nodeName.equals("attach")) {
                        String attach = child.getStringValue();
                        payInfo.setAttach(attach);
                    } else if (nodeName.equals("termId")) {
                        String termId = child.getStringValue();
                        payInfo.setTermId(termId);
                    }
                }
                payInfoList.add(payInfo);
                payInfo = null;
            }
            if(payInfoList != null && !payInfoList.isEmpty()){
                for(PayXmlInfo payInfo : payInfoList ){
                    if(code.equals(payInfo.getOrganizationCode())){
                        result =  payInfo;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = null;
        }
        return result;
    }
}
